package chan.module.project.comm;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class ClickHouseUtil {
    private static final String JDBC_PREFIX = "jdbc:ch://";
    private static final Logger log = LogManager.getLogger(ClickHouseUtil.class);

    public static void main(String[] args) {
        String url = "jdbc:ch://127.0.0.1:8123/default?dxp_auth_model=0&dxp_auth_info=&connect_timeout=60000";

        int point = url.lastIndexOf("?");

        Properties attrProperties = new Properties();
        attrProperties.put("url", url.substring(0,point) );

        String pros = url.substring(point+1);

        String[] attrs = pros.split("&");
        for (String attr : attrs){
            int attrPoint = attr.lastIndexOf("=");
            attrProperties.put( attr.substring(0,attrPoint) , attr.substring(attrPoint+1) );
        }

        System.out.println( attrProperties.size() );

    }


    public static void close(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException ignored) {
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException ignored) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ignored) {
            }
        }
    }
}
